/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tienda.zapateria.siszap.util;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import org.rx.cr.conf.Config;
import tienda.zapateria.siszap.be.BEUbigeo;
import tienda.zapateria.siszap.bl.BLUbigeo;

/**
 *
 * @author Mysham
 */
public class UtilUbigeo {

    private String defaultDepartamento;
    private String defaultProvincia;
    private String defaultDistrito;
    private Config config;

    public UtilUbigeo(Config config) {
        this.config = config;
    }

    public void recuperarDatosUbigeo(int ubigeoid, JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis) {
        BEUbigeo be = null;
        BLUbigeo bl = null;
        try {
            bl = new BLUbigeo(getConfig());
            be = bl.buscar(ubigeoid);
            
           
            defaultSelectedUbigeo(be.getDepartamento().trim(), be.getProvincia().trim(), be.getDistrito().trim(), jcbdep, jcbpro, jcbdis);
        } catch (Exception e) {
            Logger.getLogger(UtilUbigeo.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public void defaultSelectedUbigeo(String dep, String pro, String dis, JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis) {
        
        setDefaultDepartamento(dep);
        setDefaultProvincia(pro);
        setDefaultDistrito(dis);

        rellenarDepartamento(jcbdep, jcbpro, jcbdis, getDefaultDepartamento().trim(), getDefaultProvincia().trim(), getDefaultDistrito().trim());

    }

    public void selectDefaultDepartamento(JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis, String dep, String pro, String dis) {

        for (int i = 0; i < jcbdep.getItemCount(); i++) {
            BEUbigeo object = (BEUbigeo) jcbdep.getItemAt(i);
            if (i > 0 && object.toString().toLowerCase().equals(dep.toLowerCase())) {
                jcbdep.setSelectedItem(object);
            }
        }
    }

    public void selectDefaultProvincia(JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis, String dep, String pro, String dis) {

        for (int i = 0; i < jcbpro.getItemCount(); i++) {
            BEUbigeo object = (BEUbigeo) jcbpro.getItemAt(i);
            if (i > 0 && object.toString().toLowerCase().equals(pro.toLowerCase())) {
                jcbpro.setSelectedItem(object);
            }
        }
    }

    public void selectDefaultDistrito(JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis, String dep, String pro, String dis) {
        for (int i = 0; i < jcbdis.getItemCount(); i++) {
            BEUbigeo object = (BEUbigeo) jcbdis.getItemAt(i);
            if (i > 0 && object.toString().toLowerCase().equals(dis.toLowerCase())) {
                jcbdis.setSelectedItem(object);
            }
        }
    }

    public void eventoComboDepartamento(JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis) {
        if (!jcbpro.isPopupVisible()) {
            if (jcbdep.getItemCount() > 1) {
                if (jcbdep.getSelectedIndex() > 0) {
                    BEUbigeo object = (BEUbigeo) jcbdep.getSelectedItem();
                    rellenarProvincia(jcbdep, jcbpro, jcbdis, getDefaultDepartamento(), getDefaultProvincia(), getDefaultDistrito());
                } else if (jcbdep.getSelectedIndex() == 0) {
                    jcbpro.removeAllItems();
                    jcbdis.removeAllItems();

                    jcbpro.addItem(new BEUbigeo());
                    jcbdis.addItem(new BEUbigeo());

                }
            }
        }
    }

    public void eventoComboProvincia(JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis) {
        if (!jcbdis.isPopupVisible()) {
            if (jcbpro.getItemCount() > 1) {
                if (jcbpro.getSelectedIndex() > 0 && jcbdep.getSelectedIndex() > 0) {
                    BEUbigeo object = (BEUbigeo) jcbpro.getSelectedItem();

                    rellenaDistrito(jcbdep, jcbpro, jcbdis, getDefaultDepartamento(), getDefaultProvincia(), getDefaultDistrito());
                } else if (jcbpro.getSelectedIndex() == 0) {
                    jcbdis.removeAllItems();

                    jcbdis.addItem(new BEUbigeo());

                }
            }
        }
    }

    public void eventoComboDistrito(JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis) {
        if (!jcbpro.isPopupVisible()) {
            if (jcbdis.getItemCount() > 1) {
                if (jcbdis.getSelectedIndex() > 0 && jcbpro.getSelectedIndex() > 0 && jcbdep.getSelectedIndex() > 0) {
                    BEUbigeo obj = (BEUbigeo) jcbdis.getSelectedItem();
                }

            }
        }
    }

    private void rellenarDepartamento(JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis, String dep, String pro, String dis) {
        BEUbigeo be = null;
        BLUbigeo bl = null;
        try {
            bl = new BLUbigeo(config);
            jcbdep.removeAllItems();
            jcbpro.removeAllItems();
            jcbdis.removeAllItems();

            jcbdep.addItem(new BEUbigeo());
            jcbpro.addItem(new BEUbigeo());
            jcbdis.addItem(new BEUbigeo());

            ArrayList<BEUbigeo> list = bl.listarDepartamento();
            for (int i = 0; i < list.size(); i++) {
                be = list.get(i);
                be.setTipo_item(BEUbigeo.DEPARTAMENTO);
                jcbdep.addItem(be);

            }
            jcbdep.setSelectedIndex(0);
            selectDefaultDepartamento(jcbdep, jcbpro, jcbdis, dep, pro, dis);
        } catch (Exception e) {
            Logger.getLogger(UtilUbigeo.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    private void rellenarProvincia(JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis, String dep, String pro, String dis) {
        BEUbigeo be = null;
        BLUbigeo bl = null;
        try {
            jcbpro.removeAllItems();
            bl = new BLUbigeo(config);
            be = (BEUbigeo) jcbdep.getSelectedItem();
            jcbpro.removeAllItems();
            jcbpro.addItem(new BEUbigeo());

            ArrayList<BEUbigeo> list = bl.listarProvinciaDepartamento(be.getDepartamento());
            for (int i = 0; i < list.size(); i++) {
                be = list.get(i);
                be.setTipo_item(BEUbigeo.PROVINCIA);
                jcbpro.addItem(be);
            }
            jcbdis.removeAllItems();
            jcbdis.addItem(new BEUbigeo());

            jcbpro.setSelectedIndex(0);
            selectDefaultProvincia(jcbdep, jcbpro, jcbdis, dep, pro, dis);
        } catch (Exception e) {
            Logger.getLogger(UtilUbigeo.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    private void rellenaDistrito(JComboBox jcbdep, JComboBox jcbpro, JComboBox jcbdis, String dep, String pro, String dis) {
        BEUbigeo be = null;
        BEUbigeo be1 = null;

        try {
            jcbdis.removeAllItems();
            BLUbigeo bl = new BLUbigeo(config);
            be = (BEUbigeo) jcbdep.getSelectedItem();
            be1 = (BEUbigeo) jcbpro.getSelectedItem();
            jcbdis.removeAllItems();
            jcbdis.addItem(new BEUbigeo());
            ArrayList<BEUbigeo> list = bl.listarDistritoProvinciaDepartamento(be.getDepartamento(), be1.getProvincia());
            for (int i = 0; i < list.size(); i++) {
                be = list.get(i);
                be.setTipo_item(BEUbigeo.DISTRITO);
                jcbdis.addItem(be);
            }

            jcbdis.setSelectedIndex(0);
            selectDefaultDistrito(jcbdep, jcbpro, jcbdis, dep, pro, dis);
        } catch (Exception ex) {
            Logger.getLogger(UtilUbigeo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getDefaultDepartamento() {
        return defaultDepartamento;
    }

    public void setDefaultDepartamento(String defaultDepartamento) {
        this.defaultDepartamento = defaultDepartamento;
    }

    public String getDefaultProvincia() {
        return defaultProvincia;
    }

    public void setDefaultProvincia(String defaultProvincia) {
        this.defaultProvincia = defaultProvincia;
    }

    public String getDefaultDistrito() {
        return defaultDistrito;
    }

    public void setDefaultDistrito(String defaultDistrito) {
        this.defaultDistrito = defaultDistrito;
    }

    public Config getConfig() {
        return config;
    }

    public void setConfig(Config config) {
        this.config = config;
    }
}
